<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>03_防抖-this-参数绑定</title>
</head>
<body>

  <input type="text">

  <script>

    function debounce(fn, delay) {
      let timer = null;
      return function (...args) {
        if (timer) {
          clearTimeout(timer)
        }
        timer = setTimeout(() => {
          // fn.apply(this, arguments) // this指向调用debounce函数的元素，arguments是事件对象
          fn.apply(this, args)
          timer = null // 清空timer，防止内存泄漏
        }, delay)
      }
    }

    // 3.防抖处理代码
    let counter = 1
    const inputEl = document.querySelector('input')
    inputEl.oninput = debounce(function (event) {
      console.log(`发送网络请求${counter++}:`, this, event)
    }, 1000)

  </script>
  
</body>
</html>